package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.UserCoupon;
import com.ruoyi.user.service.IUserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/coupons")
public class UserCouponsController extends BaseController {

    @Autowired
    private IUserCouponService userCouponService;

    /**
     * 基本列表
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCoupon userCoupon) {
        startPage();
        List<UserCoupon> list = userCouponService.selectUserCouponList(userCoupon);
        return getDataTable(list);
    }

    /**
     * 联表详情列表（三表级联）
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:list')")
    @GetMapping("/detail/list")
    public TableDataInfo detailList(UserCoupon userCoupon) {
        startPage();
        List<Map<String, Object>> list = userCouponService.selectUserCouponDetailList(userCoupon);
        return getDataTable(list);
    }

    /**
     * 根据用户ID查询联表详情列表
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:list')")
    @GetMapping("/detail/list/{customerId}")
    public TableDataInfo detailListByCustomerId(@PathVariable Long customerId) {
        startPage();
        List<Map<String, Object>> list = userCouponService.selectUserCouponDetailListByCustomerId(customerId);
        return getDataTable(list);
    }

    /**
     * 根据用户ID和优惠券状态查询联表详情列表
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:list')")
    @GetMapping("/detail/list/{customerId}/status/{status}")
    public TableDataInfo detailListByCustomerIdAndStatus(@PathVariable Long customerId, @PathVariable Integer status) {
        startPage();
        List<Map<String, Object>> list = userCouponService.selectUserCouponDetailListByCustomerIdAndStatus(customerId, status);
        return getDataTable(list);
    }

    /**
     * 根据用户ID和优惠券类型查询联表详情列表
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:list')")
    @GetMapping("/detail/list/{customerId}/type/{type}")
    public TableDataInfo detailListByCustomerIdAndType(@PathVariable Long customerId, @PathVariable Integer type) {
        startPage();
        List<Map<String, Object>> list = userCouponService.selectUserCouponDetailListByCustomerIdAndType(customerId, type);
        return getDataTable(list);
    }

    /**
     * 获取详情
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:query')")
    @GetMapping(value = "/{userCouponId}")
    public AjaxResult getInfo(@PathVariable("userCouponId") Long userCouponId) {
        return success(userCouponService.selectUserCouponById(userCouponId));
    }

    /**
     * 新增（走发券级联）
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:add')")
    @Log(title = "用户优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCoupon userCoupon) {
        return toAjax(userCouponService.insertUserCoupon(userCoupon));
    }

    /**
     * 修改（状态为1走用券级联）
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:edit')")
    @Log(title = "用户优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCoupon userCoupon) {
        return toAjax(userCouponService.updateUserCoupon(userCoupon));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('user:coupons:remove')")
    @Log(title = "用户优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userCouponIds}")
    public AjaxResult remove(@PathVariable Long[] userCouponIds) {
        return toAjax(userCouponService.deleteUserCouponByIds(userCouponIds));
    }
}
